﻿CREATE PROCEDURE [dbo].[spListOrdersInProcess]
	@OwnerID int,
	@InventorytatusCode char(1)
AS
	declare @AllowPartialShipments bit
	select @AllowPartialShipments = AllowPartialShipments from tblOwner where OwnerID = @OwnerID

	select *
	from tblInventoryTransaction T1
		join tblOrderHeader T2 on T1.OrderID = T2.OrderID
		left outer join tblCustomerLock T3 on T2.CustomerID = T3.CustomerID
	where T1.InventoryTransactionTypeCode = 'S'
		and T1.InventoryTransactionStatusCode = @InventorytatusCode
		-- Status is paid or in process
		and T2.OrderStatusCode in ('E', 'O') 
		-- The customer is not locked for editing
		and T3.CustomerID is null 
		-- Exclude orders that are on backorder unless the owner variable AllowPartialShipments is set
		and ( @AllowPartialShipments = 1 or not exists ( select 1 from tblInventoryTransaction where OrderID = T2.OrderID and InventoryTransactionStatusCode = 'B' ) )
RETURN 0